﻿Imports System.Text.RegularExpressions

Namespace Main.Forms
    Public Class fmMainAll
        Private _mRegex As String
        Private _mReplace As String
        Private Sub fmMainAll_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            lstvFiles.Items.Clear()
            ToolStripStatusLabel.Text = ""
            _mRegex = ""
            _mReplace = ""
        End Sub

        Private Sub fmMainAll_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

        End Sub
        Private Sub btnFiles_Click(sender As Object, e As EventArgs) Handles btnFiles.Click
            With OpenFileDialog
                .Filter = "All Files|*.*"
                .InitialDirectory = Application.ExecutablePath
                .FileName = ""
                If (.ShowDialog() = DialogResult.OK) Then
                    lstvFiles.Items.Clear()
                    Dim sPath As String = ""
                    For Each fpath As String In .FileNames
                        If (sPath.Length <= 0) Then
                            sPath = IO.Path.GetDirectoryName(fpath)
                        End If
                        If (lstvFiles.Tag Is Nothing) Then
                            lstvFiles.Tag = sPath
                        Else
                            If (CStr(lstvFiles.Tag) <> sPath) Then
                                lstvFiles.Tag = sPath
                            End If
                        End If
                        lstvFiles.Items.Add(IO.Path.GetFileName(fpath))
                    Next
                End If
            End With
            lstvFiles.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
        End Sub

        Private Sub btnRule_Click(sender As Object, e As EventArgs) Handles btnRule.Click
            If (lstvFiles.Items.Count <= 0) Then
                Exit Sub
            End If
            Dim fm As FmRules = New FmRules()
            With fm
                .FileName = lstvFiles.Items(0).Text
                If (_mRegex.Length > 0) Then
                    .SRegex = _mRegex
                End If
                .SReplace = _mReplace
                If (.ShowDialog() = DialogResult.OK) Then
                    _mRegex = .SRegex
                    _mReplace = .SReplace
                    .Hide()
                    DoPreViewListBox()
                End If
                .Dispose()
            End With
        End Sub

        Private Sub btnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
            Dim n As Integer = CInt(txtDig.Text)
            n += 1
            If (n > 99) Then
                n = 1
            End If
            txtDig.Text = n.ToString()
            DoPreViewListBox()
        End Sub

        Private Sub btnNeg_Click(sender As Object, e As EventArgs) Handles btnNeg.Click
            Dim n As Integer = CInt(txtDig.Text)
            n -= 1
            If (n < 1) Then
                n = 1
            End If
            txtDig.Text = n.ToString()
            DoPreViewListBox()
        End Sub

        Private Sub btnChange_Click(sender As Object, e As EventArgs) Handles btnChange.Click
            DoChangeFileName()
        End Sub

#Region "Methods"
        Private Sub DoPreViewListBox()
            If (lstvFiles.Items.Count <= 0) Then
                Exit Sub
            End If

            Dim nDig As Integer = CInt(txtDig.Text)
            ToolStripProgressBar.Maximum = lstvFiles.Items.Count
            ToolStripProgressBar.Value = 0
            For Each item As ListViewItem In lstvFiles.Items
                If (_mRegex.Length > 0) Then
                    Dim rx As Regex = New Regex(_mRegex)
                    ToolStripStatusLabel.Text = rx.Replace(item.Text, _mReplace)
                Else
                    Try
                        Dim bCustom As Boolean = False
                        Dim strResult As String = _mReplace
                        If (_mReplace.IndexOf("%f", StringComparison.Ordinal) >= 0) Then
                            strResult = strResult.Replace("%f", item.Text.Substring(0, item.Text.LastIndexOf(".")))
                            bCustom = True
                        End If
                        If (_mReplace.IndexOf("%e", StringComparison.Ordinal) >= 0) Then
                            Dim dpos As Integer = item.Text.LastIndexOf(".") + 1
                            strResult = strResult.Replace("%e", item.Text.Substring(dpos, item.Text.Length - dpos))
                            bCustom = True
                        End If
                        If (_mReplace.IndexOf("%d", StringComparison.Ordinal) >= 0) Then
                            strResult = strResult.Replace("%d", (ToolStripProgressBar.Value + 1).ToString.PadLeft(nDig, "0"))
                            bCustom = True
                        End If

                        ToolStripStatusLabel.Text = strResult
                    Catch ex As Exception
                        ToolStripStatusLabel.Text = item.Text
                    End Try
                End If

                If (item.SubItems.Count = 1) Then
                    item.SubItems.Add(">")
                    item.SubItems.Add(ToolStripStatusLabel.Text)
                Else
                    item.SubItems(2).Text = ToolStripStatusLabel.Text
                End If

                ToolStripProgressBar.Value += 1
                Application.DoEvents()
            Next
            lstvFiles.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
        End Sub

        Private Sub DoChangeFileName()
            Dim sPath As String = lstvFiles.Tag
            ToolStripProgressBar.Maximum = lstvFiles.Items.Count
            ToolStripProgressBar.Value = 0
            For i As Integer = 0 To lstvFiles.Items.Count - 1
                Dim sOrg As String = sPath + "\" + lstvFiles.Items(i).SubItems(0).Text
                Dim sDst As String = lstvFiles.Items(i).SubItems(2).Text
                Try
                    ToolStripStatusLabel.Text = "=>" + sDst
                    ToolStripProgressBar.Value = ToolStripProgressBar.Value + 1
                    My.Computer.FileSystem.RenameFile(sOrg, sDst)
                    lstvFiles.Items(i).SubItems(1).Text = "O"
                Catch ex As Exception
                    lstvFiles.Items(i).SubItems(1).Text = "X"
                End Try
                Application.DoEvents()
            Next
        End Sub

#End Region
    End Class
End Namespace